// run: $exec < input
// flag: -g
// opt: 0
#include <iostream>
#include <set>

struct data
{
	data(long long x1, long long x2, long long x3, long long x4)
	{
		x[0] = x1; x[1] = x2; x[2] = x3; x[3] = x4;
	}
	data() {}

	long long x[4];
};

bool operator<(data const & a, data const & b)
{
	return a.x[0] < b.x[0] || (a.x[0] == b.x[0] && a.x[1] < b.x[1]) ||
		(a.x[0] == b.x[0] && a.x[1] == b.x[1] && a.x[2] < b.x[2]) ||
		(a.x[0] == b.x[0] && a.x[1] == b.x[1] && a.x[2] == b.x[2] && a.x[3] < b.x[3]);
}

std::set<data> points;
std::multiset<long long> dis[20];
long long n;

int main()
{
	std::cin >> n;
	for (long long ti = 0, t, x1, x2, x3, x4; ti < n; ti++) {
		std::cin >> t >> x1 >> x2 >> x3 >> x4;
		data p(x1, x2, x3, x4);
		if (t == 1) {
			if (points.find(p) != points.end()) continue;
			points.insert(p);
			for (long long i = 0; i < 16; i++) {
				long long sum = 0;
				for (long long j = 0; j < 4; j++) {
					long long sign = (i>>j) & 1;
					sum += (sign * 2 - 1) * p.x[j];
				}
				dis[i].insert(sum);
			}
		} else if (t == 2) {
			if (points.find(p) == points.end()) continue;
			points.erase(p);
			for (long long i = 0; i < 16; i++) {
				long long sum = 0;
				for (long long j = 0; j < 4; j++) {
					long long sign = (i>>j) & 1;
					sum += (sign * 2 - 1) * p.x[j];
				}
				dis[i].erase(sum);
			}
		} else {
			if (points.empty()) continue;
			long long ans = 0;
			for (long long i = 0; i < 16; i++) {
				long long sum1 = 0;
				for (long long k = 0; k < 4; k++) {
					long long s1 = (i>>k) & 1;
					sum1 += (s1 * 2 - 1) * p.x[k];
				}
				long long tmp = sum1 - *dis[i].begin();
				ans = std::max(ans, tmp);
			}
			std::cout << ans << '\n';
		}
	}
}

